﻿using System;
using System.Linq;
using NUnit.Framework;
using TypeDiscriminator.Model;
using Xtensive.Orm;
using Xtensive.Orm.Configuration;
using Xtensive.Orm.Providers;

namespace TypeDiscriminator
{
    public class Program
    {
        [Test]
        public static void Main()
        {
            // Loading domain configuration section with name "Default".
            var domainConfiguration = DomainConfiguration.Load("Default");
            var domain = Domain.Build(domainConfiguration);

            using (var session = domain.OpenSession()) {
                // Subscribing to Session event to display actual DbCommands
                session.Events.DbCommandExecuted += (sender, args) => Console.WriteLine(args.Command.ToHumanReadableString());

                using (var t = session.OpenTransaction()) {
                    // DataObjects.Net automatically sets Product.Kind value to 'book'
                    var book = new Book(session) {
                        Name = "DataObjects.Net unleashed",
                        Pages = 432
                    };

                    // DataObjects.Net automatically sets Product.Kind value to 'movie'
                    var movie = new Movie(session) {
                        Name = "Dogma",
                        Director = "Kevin Smith",
                        Duration = 90
                    };
                    t.Complete();
                }

                using (var tx = session.OpenTransaction()) {
                    // Fetching books. Query is translated to 'select from Producs where Kind = book'
                    var books = session.Query.All<Book>().ToList();

                    // Fetching books. Query is translated to 'select from Producs where Kind = movie'
                    var movies = session.Query.All<Movie>().ToList();

                    tx.Complete();
                }
            }
        }
    }
}
